package com.ruoyi.project.mahjong.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.mahjong.domain.vo.ReportVo;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.DeskMember;
import com.ruoyi.project.mahjong.service.IDeskMemberService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 成桌信息Controller
 *
 * @author ruoyi
 * @date 2023-07-26
 */
@RestController
@RequestMapping("/mahjong/deskmember")
public class DeskMemberController extends BaseController
{
    @Autowired
    private IDeskMemberService deskMemberService;
    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 查询成桌信息列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeskMember deskMember)
    {

        startPage();
        List<DeskMember> list = deskMemberService.selectDeskMemberList(deskMember);
        for(DeskMember member:list){
            member.setIntegral(dictDataService.selectDictLabel(member.getModeName(),member.getIntegral()));
        }
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:report')")
    @GetMapping("/report")
    public AjaxResult reportlist(DeskMember deskMember)
    {
        SecurityUtils.getLoginUser().getUser().isAdmin();
        List<SysRole> sysRoleList= SecurityUtils.getLoginUser().getUser().getRoles();
        for(SysRole sysRole:sysRoleList){
            if(sysRole.getRoleKey().equals("shopUser")||sysRole.isAdmin()){
                List<ReportVo> list = deskMemberService.reportlist(deskMember);
                return AjaxResult.success(list);
            }
        }
        deskMember.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<ReportVo> list = deskMemberService.reportlist(deskMember);
        return AjaxResult.success(list);
    }
    /**
     * 导出成桌信息列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:export')")
    @Log(title = "成桌信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DeskMember deskMember)
    {

        List<ReportVo> list = deskMemberService.reportlist(deskMember);
//        List<DeskMember> list = deskMemberService.selectDeskMemberList(deskMember);
        ExcelUtil<ReportVo> util = new ExcelUtil<ReportVo>(ReportVo.class);
        return util.exportExcel(list, "reportvo");
    }

    /**
     * 获取成桌信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deskMemberService.selectDeskMemberById(id));
    }

    /**
     * 新增成桌信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:add')")
    @Log(title = "成桌信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeskMember deskMember)
    {
        return toAjax(deskMemberService.insertDeskMember(deskMember));
    }

    /**
     * 修改成桌信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:edit')")
    @Log(title = "成桌信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeskMember deskMember)
    {
        return toAjax(deskMemberService.updateDeskMember(deskMember));
    }

    /**
     * 删除成桌信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:deskmember:remove')")
    @Log(title = "成桌信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deskMemberService.deleteDeskMemberByIds(ids));
    }
}
